package com.ugrow.ugrowcommunity.activity.warranty;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SearchView;
import android.widget.Toolbar;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ugrow.ugrowcommunity.R;

import java.util.ArrayList;
import java.util.List;

public class WarrantyActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private List<NoteBean> mNotes;
    private MyAdapter myAdapter;

    private MyOpenHelper myOpenHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_warranty);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void onResume() {
        super.onResume();

        refreshDataFromDB();
    }

    private void refreshDataFromDB() {
        mNotes = getDataFromDB();
        myAdapter.refreshData(mNotes);
    }

    private void initEvent() {
        myAdapter = new MyAdapter(this, mNotes);

        mRecyclerView.setAdapter(myAdapter);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
    }

    private void initData() {
        mNotes = new ArrayList<>();
        myOpenHelper = new MyOpenHelper(this);

//        for (int i = 0; i < 30; i++) {
//            NoteBean noteBean = new NoteBean();
//            noteBean.setTitle("这是标题");
//            noteBean.setContent("这是内容");
//            noteBean.setCreatedTime(getCurrentTimeFormat());
//            mNotes.add(noteBean);
//        }
        //从数据库中获取数据，刷新页面


    }

    private List<NoteBean> getDataFromDB() {
        return myOpenHelper.queryAll();

    }

//    private String getCurrentTimeFormat(){
//        SimpleDateFormat simpleDateFormat = null;
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
//            simpleDateFormat = new SimpleDateFormat("YYYY年MM月dd HH:mm:ss");
//        }
//        Date date = new Date();
//        return simpleDateFormat.format(date);
//    }

    public void initView() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        mRecyclerView = findViewById(R.id.rtl);
    }

    public void add(View view) {
        Intent intent = new Intent(this, AddActivity.class);
        startActivity(intent);
    }


    //菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                mNotes = myOpenHelper.queryByTitle(newText);

                //刷新列表
                myAdapter.refreshData(mNotes);
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {// 点击返回图标事件
            this.finish();
        }
        return super.onOptionsItemSelected(item);
    }


}